﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02._PRQS.AddPRQSData
{
    /// <summary>
    /// 新增检疫项目表
    /// </summary>
    public class AddPRQSDataHandler : IRequestHandler<AddPRQSDataCommand, Result>
    {
        public IBaseRepository<PRQSData> PRQSDataRepository { get; set; }
        public IMapper mapper { get; set; }
        public AddPRQSDataHandler(IBaseRepository<PRQSData> pRQSDataRepository, IMapper mapper)
        {
            PRQSDataRepository = pRQSDataRepository;
            this.mapper = mapper;
        }
        public async Task<Result> Handle(AddPRQSDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            PRQSData data = mapper.Map<PRQSData>(request);
            var list = PRQSDataRepository.GetAll().Where(x => x.PRQSName == request.PRQSName).Count();
            if (list == 0)
            {
                var AddNum = PRQSDataRepository.Add(data);
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
    }
}
